package cn.liit.weixin.redpack.support;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import cn.liit.weixin.core.config.Config;
import cn.liit.weixin.core.utils.Signature;
import cn.liit.weixin.redpack.model.Redpack;
import name.zbq.bush.utils.MD5Util;
import name.zbq.bush.utils.RandomStringGenerator;
import name.zbq.bush.utils.UnitUtil;
import name.zbq.bush.utils.http.HttpConnect;

/**
 * 发送给红包提供类(发送红包要配置证书文件)
 * @author zhangboqi
 */
public class RedpackSupport {
	public static String APIURL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
	/**
	 * @param orderNo 商户订单号
	 * @param re_openid 接受红包的openid
	 * @param amount 红包金额 单位元
	 * @param path 证书路径
	 * @param partner 商户号
	 * @return
	 */
	public static Map<String,String> sendRedPack(Redpack redpack){
		String nonce_str = RandomStringGenerator.getNonceStr();
		//--------------------签名参数
		SortedMap<String,String> signParam = new TreeMap<String, String>();
		signParam.put("nonce_str", nonce_str);
		signParam.put("mch_billno", redpack.getOrderNo());
		signParam.put("mch_id", Config.partner);
		signParam.put("wxappid", Config.appid);
		signParam.put("send_name", redpack.getSend_name());
		signParam.put("re_openid", redpack.getRe_openid());
		signParam.put("total_amount", UnitUtil.getMoney(redpack.getAmount()));
		signParam.put("total_num", redpack.getTotal_num());
		signParam.put("wishing", redpack.getWishing());
		signParam.put("client_ip", redpack.getClient_ip());
		signParam.put("act_name", redpack.getAct_name());
		signParam.put("remark", redpack.getRemark());
		
		//-----------------------参数
		String sign = Signature.createSign(signParam,"UTF-8");
		SortedMap<String,String> reqParams = new TreeMap<String, String>();
		reqParams.put("nonce_str", nonce_str);
		reqParams.put("sign",sign);
		reqParams.put("mch_billno", redpack.getOrderNo());
		reqParams.put("mch_id", Config.partner);
		reqParams.put("wxappid", Config.appid);
		reqParams.put("send_name", redpack.getSend_name());
		reqParams.put("re_openid", redpack.getRe_openid());
		reqParams.put("total_amount", UnitUtil.getMoney(redpack.getAmount()));
		reqParams.put("total_num", redpack.getTotal_num());
		reqParams.put("wishing", redpack.getWishing());
		reqParams.put("client_ip", redpack.getClient_ip());
		reqParams.put("act_name", redpack.getAct_name());
		reqParams.put("remark",  redpack.getRemark());
		Map<String,String> resultData = HttpConnect.getInstance().doPostXml(APIURL , reqParams);
		return resultData;
	}
	public static void main(String[] args) {
		//System.out.println(sendRedPack("1235585","okyoyuHLHcY5z1JCq1guLBbQO4JI", "1"));
	}
}
